package com.nulldev.util.web.HttpServer3.extensions.nCaptcha.backend;

import com.nulldev.util.web.HttpServer3.Response;
import com.nulldev.util.web.HttpServer3.extensions.nCaptcha.backend.user.User;
import com.nulldev.util.web.HttpServer3.extensions.nCaptcha.backend.user.UserManager;
import com.nulldev.util.web.HttpServer3.extensions.nCaptcha.backend.util.UUIDCookieLoader;
import com.nulldev.util.web.HttpServer3.pages.v3.HttpPage3;

public class RemoveCaptchaState extends HttpPage3 {

	public RemoveCaptchaState() {
		super("/captcha/dev/remove_captcha", true);
		super.setCachingMethod(CachingMethod.NO_CACHE);
	}

	@Override
	public Response onRequest(final Request request) {
		final User user = UUIDCookieLoader.userFromRequest(request);
		if (user == null) {
			return new Response(401, createStream("/dev/remove_captcha: Request rejected."));
		}

		if (user.isNotBot()) {
			if (UserManager.removeUser(user)) {
				return new Response(200, createStream("/dev/remove_captcha: Removed."));
			} else {
				return new Response(200, createStream("/dev/remove_captcha: Failed to remove user."));
			}
		} else {
			return new Response(401, createStream("/dev/remove_captcha: Request rejected."));
		}
	}

}
